package com.just.gameapp.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.just.gameapp.entity.AccountStrategy;
import com.just.gameapp.entity.Strategy;
import com.just.gameapp.mapper.AccountStrategyMapper;
import com.just.gameapp.mapper.StrategyMapper;
import com.just.gameapp.service.StrategyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zyr
 * @date 2024/6/18 下午3:03
 * @Description
 */
@Slf4j
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy> implements StrategyService {
    @Resource
    StrategyMapper strategyMapper;
    @Resource
    AccountStrategyMapper accountStrategyMapper;

    @Override
    public List<Strategy> getStrategyByGameName(String gameName) {
        LambdaQueryWrapper<Strategy> query = new LambdaQueryWrapper<>();
        query.like(Strategy::getGameName, gameName);
        List<Strategy> strategies = strategyMapper.selectList(query);
        return Objects.requireNonNullElseGet(strategies, List::of);
    }

    @Override
    public List<Strategy> selectStrategyList() {
        List<Strategy> strategies = strategyMapper.selectList(null);
        // 获取到的数据根据id从大到小排序
        strategies.sort((o1, o2) -> o2.getStrategyId() - o1.getStrategyId());
        return strategies;
    }

    @Override
    public List<String> getStrategyByAccountId(String accountId) {
        LambdaQueryWrapper<AccountStrategy> query = new LambdaQueryWrapper<>();
        query.eq(AccountStrategy::getAccountId, accountId);
        // 查询该用户对应的攻略信息
        List<String> strategies = accountStrategyMapper.selectList(query)
                .stream()
                .map(m-> m.getStrategyId().toString())
                .toList(); // 收集为 List<Strategy>

        return strategies;
    }

    /**
     * 保存攻略，并保存与用户对应关系
     * @param strategy
     * @param accountId
     * @return
     */
    @Override
    public String saveStrategy(Strategy strategy,String accountId) {
        int insert = strategyMapper.insert(strategy);
        if (insert > 0) {
            // 新增攻略
            LambdaQueryWrapper<Strategy> query = new LambdaQueryWrapper<>();
            query.eq(Strategy::getStrategyName, strategy.getStrategyName())
                    .eq(Strategy::getGameName, strategy.getGameName())
                    .eq(Strategy::getStrategyContent, strategy.getStrategyContent());
            Integer strategyId = strategyMapper.selectOne(query).getStrategyId();
            // 新增用户与攻略关系
            AccountStrategy accountStrategy = new AccountStrategy();
            accountStrategy.setAccountId(Integer.parseInt(accountId));
            accountStrategy.setStrategyId(strategyId);
            int insert2 = accountStrategyMapper.insert(accountStrategy);
            if(insert2 > 0){
                return strategyId.toString();
            }
        }
        return "保存失败";
    }

    @Override
    public boolean deleteByChoice(String strategyId) {
        int i = strategyMapper.deleteById(strategyId);
        return i > 0;
    }
}
